package com.eastshop.product.adminaction;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.eastshop.category.service.CategorySerive;
import com.eastshop.category.vo.Category;
import com.eastshop.product.service.ProductService;
import com.eastshop.product.vo.Product;
import com.eastshop.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 后台商品的管理的Action类
 * @author admin
 *
 */
public class AdminProductAction extends ActionSupport implements ModelDriven<Product>{
	// 模型驱动使用的对象
	private Product product = new Product();

	public Product getModel() {
		return product;
	}
	
	// 注入商品的Service
	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	// 注入商品分类的Service
	private CategorySerive categorySerive;
	
	public void setCategorySerive(CategorySerive categorySerive) {
		this.categorySerive = categorySerive;
	}

	// 接收page
	private int page;

	public void setPage(int page) {
		this.page = page;
	}
	
	// 文件上传需要的三个属性:
	private File upload;
	private String uploadFileName;
	private String uploadContentType;

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	// 分页查询商品信息
	public String findAllWithPage(){
		// 分页查询商品信息
		PageBean<Product> pageBean = productService.findAllWithPage(page);
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAllWithPage";
	}
	
	// 删除商品
	public String delete(){
		// 根据id查询商品信息
		product = productService.findByPid(product.getPid());
		// 删除图片文件
		String path = ServletActionContext.getServletContext().getRealPath("/"+product.getImage());
		File file = new File(path);
		file.delete();
		// 删除数据库中的记录
		productService.delete(product);
		return "delete";
	}
	
	// 跳转到添加商品的页面
	public String add(){
		// 查询所有商品分类
		List<Category> cList = categorySerive.find();
		ActionContext.getContext().getValueStack().set("cList", cList);
		return "add";
	}
	
	// 保存添加商品的表单
	public String addSubmit() throws IOException{
		// 将提交的数据添加到数据库中
		product.setPdate(new Date());
		if(upload != null){
			// 将图片上传到服务器端上
			// 获取上传图片的路径
			String path = ServletActionContext.getServletContext().getRealPath("/products");
			// 创建文件类型对象
			File diskFile = new File(path+"//"+uploadFileName);
			// 文件上传
			FileUtils.copyFile(upload, diskFile);
			product.setImage("products/"+uploadFileName);
		}
		productService.save(product);
		return "addSubmit";
	}
	
	// 跳转到修改商品的页面
	public String edit(){
		// 根据id查询商品
		product = productService.findByPid(product.getPid());
		// 查询所有商品分类
		List<Category> cList = categorySerive.find();
		ActionContext.getContext().getValueStack().set("cList", cList);
		return "edit";
	}
	
	// 保存修改商品的表单
	public String editSubmit() throws IOException{
		product.setPdate(new Date());
		// 上传
		if(upload != null){
			// 删除原来的图片
			String delPath = ServletActionContext.getServletContext().getRealPath("/"+product.getImage());
			File delFile = new File(delPath);
			delFile.delete();
			// 上传新图片
			// 获取上传的路径
			String path = ServletActionContext.getServletContext().getRealPath("/products");
			File file = new File(path+"//"+uploadFileName);
			FileUtils.copyFile(upload, file);
			product.setImage("products/"+uploadFileName);
		}
		productService.update(product);
		return "editSubmit";
	}
}
